package logMonitor.utils;

import logMonitor.dao.DataSourceUtil;
import logMonitor.dao.LogMonitorDao;
import logMonitor.domain.Message;
import logMonitor.domain.Record;
import logMonitor.domain.Rule;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/1/15.
 */
public class SUtest {
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(DataSourceUtil.getDataSource());
    public static void main(String[] args) {

        //
        Message message = handler2.parser("123");

        List<Rule> ruleListAlive = new LogMonitorDao().loadRuleAlive();
        new SUtest().delete(ruleListAlive.get(2).getId());
//        List<Rule> ruleListDead = new LogMonitorDao().loadRuleDead();
//        for(Rule rule:ruleListAlive){
//            System.out.println(rule.toString());
//        }
//        List<Record> recordList = new ArrayList<Record>();
//        List<Rule> ruleList= new LogMonitorDao().loadRuleAlive();
//
//        for(Rule rule :ruleList){
//            System.out.println(rule);
//        }
//        Record record = new Record();
//        for(int i=0;i<4;i++){
//            new SUtest().delete( new Integer(i));
//        }


    }

    public void delete(Integer personId) {
        jdbcTemplate .update( "delete from `log_monitor`.`rule` where `id`=?" ,
                new Object[] { personId },
                new int [] { java.sql.Types. INTEGER });
    }
    public int[][] saveRecord(List<Record> recordList) {
        String sql = "delete from `log_monitor`.`record` where `id`=?";
        return saveAppendRecord(recordList, sql);
    }

    public int[][] saveAppendRecord(List<Record> recordList, String sql) {
        return jdbcTemplate.batchUpdate(sql, recordList, recordList.size(), new ParameterizedPreparedStatementSetter<Record>() {
            @Override
            public void setValues(PreparedStatement ps, Record argument) throws SQLException {
                ps.setInt(1, argument.getId());
            }
        });
    }
}
